package com.tekj.cxqcb.view.Module.activity.StoreModule;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.operation.resultBean.SelectByIdBean;
import com.tekj.cxqcb.view.Module.adapter.StorePhotoAdapter;
import com.tekj.cxqcb.view.Module.decoration.SpacesItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.店铺信息修改;

/**
 * 店铺详情
 */
public class StoreDetailsActivity extends BaseActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_adderss)
    TextView tvAdderss;
    @BindView(R.id.tv_detailed_adderss)
    EditText tvDetailedAdderss;
    @BindView(R.id.rv_photo)
    RecyclerView rvPhoto;
    @BindView(R.id.tv_ztyy)
    TextView tvZtyy;
    @BindView(R.id.tv_xgxx)
    TextView tvXgxx;
    @BindView(R.id.tv_store_id)
    TextView tvStoreId;
    @BindView(R.id.img_photo1)
    ImageView imgPhoto1;

    private StorePhotoAdapter adapter;
    private SelectByIdBean.DataBeanX.DataBean data;
    private BusinessDao businessDao;

    @Override
    public void init() {
        tvTitle.setText("店铺资料");
        EventBus.getDefault().register(this);
        businessDao = new BusinessDao(activity, this);

        data = (SelectByIdBean.DataBeanX.DataBean) getIntent().getSerializableExtra("data");

        adapter = new StorePhotoAdapter(null);
        rvPhoto.setLayoutManager(new GridLayoutManager(activity, 3));
        rvPhoto.setAdapter(adapter);
        rvPhoto.addItemDecoration(new SpacesItemDecoration(10));

        setViewData(data);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_store_details;
    }

    boolean isFirst;

    @Override
    public void initLoadStart() {
        if (isFirst) {
            loading.show();
            isFirst = false;
        } else
            showDialog();
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 通过id查找店铺:
                SelectByIdBean byIdBean = (SelectByIdBean) bindingViewBean.getBean();
                if ("0000000".equals(byIdBean.getCode())) {
                    setViewData(byIdBean.getData().getData());
                } else {
                    Toasty.normal(activity, byIdBean.getMsg()).show();
                }

                break;
            case 修改店铺状态:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if ("0000000".equals(pubStatusBean.getCode())) {
                    EventBus.getDefault().post(new EventBusMsgBean(店铺信息修改, ""));
                } else
                    Toasty.normal(activity, pubStatusBean.getMsg()).show();
                break;
        }
    }


    @OnClick({R.id.ll_back, R.id.tv_ztyy, R.id.tv_xgxx})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_ztyy:
                if (state == 0) {
                    Toasty.normal(activity, "店铺未开启请联系管理员").show();
                    return;
                }
                Tools.showNormalDialogOne(activity, "营业状态", state == 1 ? "是否立即营业" : "是否暂停营业",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                businessDao.ApiShopupdateShopStateById(data.getId(), state);
                            }
                        }, null);

                break;
            case R.id.tv_xgxx:
                Intent intent = new Intent(activity, ModifyShopActivity.class);
                intent.putExtra("data", data);
                startActivity(intent);
                break;
        }
    }

    String[] shopStates = {"店铺未开启", "暂停营业", "立即营业", "已关闭"};
    int state;

    /**
     * 设置界面中店铺信息
     */
    private void setViewData(SelectByIdBean.DataBeanX.DataBean bean) {
        tvStoreId.setText("店铺ID：" + bean.getId());
        tvName.setText(bean.getShopName());
        tvPhone.setText(bean.getShopTel());
        tvAdderss.setText(bean.getProvinceName() + bean.getCityName() + bean.getRegionName());
        tvDetailedAdderss.setText(bean.getAddress().replace(",", " "));
        tvZtyy.setText(shopStates[bean.getShopState()]);

        Tools.picassoNetwork(bean.getAuthorization(), imgPhoto1);

        if (bean.getShopState() == 1)
            state = 2;
        else if (bean.getShopState() == 2)
            state = 1;
        else
            state = 0;

        if (bean.getShopState() == 3) {
            tvZtyy.setVisibility(View.GONE);
            tvXgxx.setVisibility(View.GONE);
        }
        adapter.setNewData(Arrays.asList(bean.getShopImage()));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void Event(EventBusMsgBean e) {
        switch (e.getmEnumStatus()) {
            case 店铺信息修改:
                businessDao.selectAll(MainBActivity.User.getUserId());
                break;
        }
    }

}
